package com.weizhi.server.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.weizhi.server.enums.ResponseCode;

import java.io.Serializable;


//忽略空值，空值不进行序列化操作
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseDto<T> implements Serializable{


    private static final long serialVersionUID = 1L;

    private int status;
    private String msg;
    private T data;

    private ResponseDto(int status){
        this.status = status;
    }

    private ResponseDto(int status, String msg){
        this.status = status;
        this.msg = msg;
    }

    private ResponseDto(int status, T data){
        this.status = status;
        this.data = data;
    }

    private ResponseDto(int status, String msg, T data){
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    //json忽略此属性
    @JsonIgnore
    public boolean isSuccess(){
        return status == ResponseCode.SUCCESS.getCode();
    }

    public static <T> ResponseDto<T> createBySuccess(){
        return new ResponseDto<T>(ResponseCode.SUCCESS.getCode());
    }
    public static <T> ResponseDto<T> createBySuccessMessage(String msg){
        return new ResponseDto<T>(ResponseCode.SUCCESS.getCode(), msg);
    }
    public static <T> ResponseDto<T> createBySuccess(T data){
        return new ResponseDto<T>(ResponseCode.SUCCESS.getCode(), data);
    }
    public static <T> ResponseDto<T> createBySuccess(String msg, T data){
        return new ResponseDto<T>(ResponseCode.SUCCESS.getCode(), msg, data);
    }

    public static <T> ResponseDto<T> createByError(){
        return new ResponseDto<T>(ResponseCode.ERROR.getCode());
    }
    public static <T> ResponseDto<T> createByErrorMessage(String msg){
        return new ResponseDto<T>(ResponseCode.ERROR.getCode(), msg);
    }

    public static <T> ResponseDto<T> createByErrorCode(int code){
        return new ResponseDto<T>(code);
    }

    public static <T> ResponseDto<T> createByErrorCode(int code, String msg){
        return new ResponseDto<T>(code, msg);
    }



    public int getStatus() {
        return status;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }

}
